import {customer} from '../api/service'
import {useAddkey}from '../hooks/index'
import {timestampToTime,randomString} from '../untils/index'
const IndexModel = {
  namespace: 'kefu',

  state: {
    customer:[]
  },
// 异步任务
  effects: {
    *GET_KEFU({payload},{call,put}){
        const data = yield customer({payload})
        yield put({type:'SET_KEFU', payload:data.data})
    }
  },
  reducers: {
    SET_KEFU(state, {payload}) {
        state.customerlist= useAddkey(payload)
      return {
        ...state,
       
      };
   
    },
    SEARCH(state, payload) {
      const searcharr = state.customerlist.filter((item)=>{
        return item.kefu_name.includes(payload.value)
      })
      return{
        ...state,
        customerlist: searcharr
      }
    },
     //   删除数据
    //  DEL(state,{payload}){
    //     console.log(payload,'payload')
    //     const newState = JSON.parse(JSON.stringify(state))
    //    const index=  newState.customerlist.findIndex(item=>item.kefu_id === payload.kefu_id)
    //     newState.customerlist.splice(index,1)
    //     return {
    //         ...newState
    //     }
    // },
    DEL(state, { payload }) {
      // console.log(state, 'state', payload, 'payload');
      const newState = JSON.parse(JSON.stringify(state));
      newState.customerlist.splice(payload, 1);
      return {
        ...newState,
      };
    },
    ADD(state,{payload}){
      // console.log(state)
      const {kefu_password,kefu_name} = payload
      const newState = JSON.parse(JSON.stringify(state))
      newState.customerlist.push({
        key: newState.customerlist[newState.customerlist.length - 1].kefu_id + 1,
        kefu_id: newState.customerlist[newState.customerlist.length - 1].kefu_id + 1,
        kefu_name: kefu_name,
        kefu_password: kefu_password,
        kefu_code: 'kefu' + randomString(13),
        kefu_avatar: '/static/common/images/kefu.jpg',
        online_status: 2,
        create_time: timestampToTime(new Date().getTime()),
        update_time: timestampToTime(new Date().getTime()),
        client_id: '',
      })
      return{
        ...newState
      }
      
    },
    EDIT(state,{payload}){
      // console.log(state)
         const {editIndex,kefu_name} = payload
         const newState = JSON.parse(JSON.stringify(state))
         newState.customerlist[editIndex].kefu_name = kefu_name
        //  console.log(new)
         return{
          ...newState
         }
    }
    // 启用 immer 之后
    // save(state, action) {
    //   state.name = action.payload;
    // },
  },
 
};

export default IndexModel;